package com.ruoyi.college.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.college.domain.CourseArrangement;
import com.ruoyi.college.domain.Teacher;
import com.ruoyi.college.service.ICourseArrangementService;
import com.ruoyi.college.service.ITeacherService;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.college.domain.CourseTeacher;
import com.ruoyi.college.service.ICourseTeacherService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 授课老师安排Controller
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@Controller
@RequestMapping("/college/courseTeacher")
public class CourseTeacherController extends BaseController
{
    private String prefix = "college/courseTeacher";

    @Autowired
    private ICourseTeacherService courseTeacherService;

    @Autowired
    private ICourseArrangementService courseArrangementService;

    @Autowired
    private ITeacherService teacherService;

    @RequiresPermissions("college:courseTeacher:view")
    @GetMapping()
    public String courseTeacher(ModelMap mmap)
    {
        List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        mmap.put("courseArrangements", courseArrangements);
        List<Teacher> teachers = teacherService.selectTeacherList(new Teacher());
        mmap.put("teachers", teachers);
        return prefix + "/courseTeacher";
    }

    /**
     * 查询授课老师安排列表
     */
    @RequiresPermissions("college:courseTeacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseTeacher courseTeacher)
    {
        startPage();
        List<CourseTeacher> list = courseTeacherService.selectCourseTeacherList(courseTeacher);
        return getDataTable(list);
    }

    /**
     * 导出授课老师安排列表
     */
    @RequiresPermissions("college:courseTeacher:export")
    @Log(title = "授课老师安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseTeacher courseTeacher)
    {
        List<CourseTeacher> list = courseTeacherService.selectCourseTeacherList(courseTeacher);
        ExcelUtil<CourseTeacher> util = new ExcelUtil<CourseTeacher>(CourseTeacher.class);
        return util.exportExcel(list, "授课老师安排数据");
    }

    /**
     * 新增授课老师安排
     */
    @RequiresPermissions("college:courseTeacher:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        mmap.put("courseArrangements", courseArrangements);
        List<Teacher> teachers = teacherService.selectTeacherList(new Teacher());
        mmap.put("teachers", teachers);
        // 关键：即使是新增页面，也需要初始化一个空列表避免NullPointerException
        mmap.put("checkedTeacherIds", new ArrayList<>()); // 新增时传递空列表

        // 编辑时传递实际选中的ID列表
        //if (StringUtils.isNotNull(courseId)) {
        //    List<Long> checkedTeacherIds = courseTeacherService.selectTeacherIdsByCourseId(courseId);
        //    mmap.put("checkedTeacherIds", checkedTeacherIds); // 编辑时传递选中的ID
        //}
        return prefix + "/add";
    }

    /**
     * 新增保存授课老师安排
     */
    @RequiresPermissions("college:courseTeacher:add")
    @Log(title = "授课老师安排", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseTeacher courseTeacher)
    {
        return toAjax(courseTeacherService.insertCourseTeacher(courseTeacher));
    }

    /**
     * 修改授课老师安排
     */
    @RequiresPermissions("college:courseTeacher:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        mmap.put("courseArrangements", courseArrangements);
        List<Teacher> teachers = teacherService.selectTeacherList(new Teacher());
        mmap.put("teachers", teachers);
        CourseTeacher courseTeacher = courseTeacherService.selectCourseTeacherById(id);
        mmap.put("courseTeacher", courseTeacher);
        return prefix + "/edit";
    }

    /**
     * 修改保存授课老师安排
     */
    @RequiresPermissions("college:courseTeacher:edit")
    @Log(title = "授课老师安排", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseTeacher courseTeacher)
    {
        return toAjax(courseTeacherService.updateCourseTeacher(courseTeacher));
    }

    /**
     * 删除授课老师安排
     */
    @RequiresPermissions("college:courseTeacher:remove")
    @Log(title = "授课老师安排", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(courseTeacherService.deleteCourseTeacherByIds(ids));
    }

    @Log(title = "授课老师安排", businessType = BusinessType.DELETE)
    @GetMapping( "/getTeachersByArrangementId")
    @ResponseBody
    public AjaxResult getTeachersByArrangementId(Integer arrangementId)
    {
        CourseTeacher courseTeacher = new CourseTeacher();
        courseTeacher.setArrangementId(Long.valueOf(arrangementId));
        List<CourseTeacher> courseTeachers = courseTeacherService.selectCourseTeacherList(courseTeacher);
        return AjaxResult.success(courseTeachers);
    }
}
